package psp.linearRegression;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;

public class LinearRegressionFile {
	
	private LinkedList<Float> xNumbers = null;
	private LinkedList<Float> yNumbers = null;

	private String fileName = null;
	
	private String xStringNumbers = null;
	private String yStringNumbers = null;
	
	/**
	 * Read a file with numbers that will be used in the linear regression calculation
	 * @param fileName Name of file that will be read
	 * @throws IncorrectNumberOfSetsException Exception thrown when the file has more than two lines
	 */
	public LinearRegressionFile(String fileName) throws IncorrectNumberOfSetsException {
		// Read the file and put each of the two lines in two LinkedLists
		this.fileName = fileName;
		this.readFile();
		
		this.xNumbers = new LinkedList<Float>(); 
		this.readArray(this.xNumbers, this.xStringNumbers);
		
		this.yNumbers = new LinkedList<Float>();
		this.readArray(this.yNumbers, this.yStringNumbers);		
	}
	
	/**
	 * Read the file with two lines and include them in the proper variable
	 * @throws IncorrectNumberOfSetsException Exception thrown when the file has more than two lines
	 */
	private void readFile() throws IncorrectNumberOfSetsException {
		try {
			FileInputStream fin = new FileInputStream(this.fileName);
			DataInputStream din = new DataInputStream(fin);
			
			String line = null;
			
			if (( line = din.readLine()) != null)
				this.xStringNumbers = line.trim();
			
			if (( line = din.readLine()) != null)
				this.yStringNumbers = line.trim();
			
			if (( line = din.readLine()) != null)
				throw new IncorrectNumberOfSetsException(IncorrectNumberOfSetsException.MORE_THAN_TWO_LINES);
			
			if (this.yStringNumbers == null)
				throw new IncorrectNumberOfSetsException(IncorrectNumberOfSetsException.LESS_THAN_TWO_LINES);
			
			din.close();
		} catch (FileNotFoundException ex) {
			System.err.println("The file entered is not valid!");
		} catch (IOException ex){
		    ex.printStackTrace();
	    }
	}
	
	/**
	 * Read the values of one line and includes in one LinkedList
	 * @param list List in which the numbers will be included
	 * @param numbersInString String containing the number to be included
	 */
	private void readArray(LinkedList<Float> list, String numbersInString) {
		
		String[] numbers = numbersInString.split(" ");
		
		try {
			for (int i = 0; i < numbers.length; i++)
				if (! numbers[i].trim().equals(""))
					list.add(Float.parseFloat(numbers[i].trim()));
		} catch (NumberFormatException ex) {
	    	System.err.println("There is an incorrect number in your file: " + ex.getMessage());
	    	System.exit(0);
		}
	}
	
	/**
	 * Returns the collection of x numbers used in the linear regression
	 * @return X numbers
	 */
	LinkedList<Float> getXNumbers() {
		return this.xNumbers;
	}
	
	/**
	 * Returns the collection of y numbers used in the linear regression
	 * @return Y numbers
	 */
	LinkedList<Float> getYNumbers() {
		return this.yNumbers;
	}
}
